//
//  UserNewtaskController.m
//  CheekiOS
//
//  Created by mac on 2017/6/30.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserNewtaskController.h"
#import "UserNewTaskCell.h"

//完善资料
#import "UserEditInfoCollionView.h"
//第一次消费  视频聊天
#import "CheekVodepViewController.h"
#import "MZCTabBarController.h"
//充值
#import "UserAccountViewController.h"
//实名认证
#import "UserRealNameController.h"
//朋友圈
#import "FriendLineViewController.h"
//视频认证
#import "UserRealVideoController.h"
//分享
#import "CustomShareView.h"
#import "WXApi.h"
#import "TencentMessageObject.h"
#import "QQApiInterface.h"
#import "WeiboSDK.h"




#define sharUrl @"http://192.168.1.210:8092/fenxiang.html"

@interface UserNewtaskController ()<UITableViewDelegate, UITableViewDataSource,UserNewTaskCellDelegate,CustomShareViewDelegate,WXApiDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dateSource;

@property (nonatomic, strong)UserNewTaskModel *TaskModel;

@property (nonatomic, assign)NSInteger page;
@property (nonatomic, assign)NSInteger rows;

@property (nonatomic, strong) CustomShareView *customShareView;//分享

@property (nonatomic, copy)NSString *taskId;

@end

@implementation UserNewtaskController

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [self requestDate];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 64)];
    backView.backgroundColor = Colorbackground;
    [self.view addSubview:backView];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(22, 19.5, 44, 44)];
    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置，现设为居左，默认的是居中
    
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 5.5, 0, 0);
    [btn addTarget:self action:@selector(backActionbtn) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:btn];
    
    self.view.backgroundColor = [UIColor whiteColor];
    

    

    self.title = @"新手任务";
    _page = 1;
    _rows = 10;
    _taskId = @"";
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGB(246, 246, 246);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    _customShareView = [[CustomShareView alloc]init];
    
    _customShareView.delegate = self;
    
    [self.view addSubview:_customShareView];
    
}
- (void)backActionbtn{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requestDate{
    
    [HcardHttpmanager getTaskReceiveWithUid:[HQHttpManager getUserId] WithPage:_page WithRows:_rows success:^(id result) {
        
        if ([result[@"code"] isEqualToString:@"ok"]) {
            
            [_dateSource removeAllObjects];
            
            _dateSource = [UserNewTaskModel mj_objectArrayWithKeyValuesArray:result[@"res"]];
            
            [self.tableView reloadData];
        }
    
    } failure:^(NSError *error) {
        
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dateSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserNewTaskCell *cell = [UserNewTaskCell cellWithTableView:tableView indexPath:indexPath];
    cell.delegate = self;
    cell.backgroundColor = RGB(246, 246, 246);
    
    cell.Model = _dateSource[indexPath.row];
    
    
    
     
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 175;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


}


- (void)UserNewTaskButtonClick:(UIButton *)button{
    
    WEAKSELF
    UserNewTaskModel *model = _dateSource[button.tag];
    
    if ([model.status isEqualToString:@"2"]) {
        return;
    }else if ([model.status isEqualToString:@"0"]) {
        
        if ([model.type isEqualToString:@"1"]) {
            
            UserEditInfoCollionView *vc = [[UserEditInfoCollionView alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if ([model.type isEqualToString:@"2"]){
        
            
            MZCTabBarController * rootVc = (MZCTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            rootVc.selectedIndex = 1;
            
            [self.navigationController popToRootViewControllerAnimated:YES];

        }else if ([model.type isEqualToString:@"3"]){
            
            UserAccountViewController *vc = [[UserAccountViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if ([model.type isEqualToString:@"4"]){
            _taskId = [NSString stringWithFormat:@"%@",model.ID];

            [self shareConnectionAction];

            
        }else if ([model.type isEqualToString:@"5"]){
            
            UserRealNameController *vc = [[UserRealNameController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if ([model.type isEqualToString:@"6"]){
            
            UserEditInfoCollionView *vc = [[UserEditInfoCollionView alloc] init];
            
            [self.navigationController pushViewController:vc animated:YES];

        }else if ([model.type isEqualToString:@"7"]){
            
            FriendLineViewController *vc = [[FriendLineViewController alloc] init];
            vc.isFriend = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }else if ([model.type isEqualToString:@"7"]){
            
            UserRealVideoController *vc = [[UserRealVideoController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
        
            MZCTabBarController * rootVc = (MZCTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            rootVc.selectedIndex = 1;
            
            [self.navigationController popToRootViewControllerAnimated:YES];
            

        }
    }else if ([model.status isEqualToString:@"1"]) {
        
        [HcardHttpmanager getreceiveCharmWithtaskId:model.ID WithUid:[HQHttpManager getUserId] WithCharm:model.charm success:^(id result) {
            if ([result[@"code"] isEqualToString:@"ok"]) {
                NSLog(@"领取成功");
                [weakSelf requestDate];
            }
        } failure:^(NSError *error) {
            
        }];
    }
    
    
}



#pragma mark ============邀请好友=====
- (void)shareConnectionAction{
    
    [_customShareView showInView:self.view.window];
    
    
}
- (void)shareControl:(UIButton *)sender{
    [_customShareView hideInView];
    NSLog(@"%ld",(long)sender.tag);
    if (sender.tag == 0){
        [self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
        
        
    }else if (sender.tag == 1){
        [self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
        
    }else if (sender.tag == 2){
        [self shareWebPageToPlatformType:UMSocialPlatformType_Qzone];
        
        
        
    }else if (sender.tag == 3){
        
        //         WBAuthorizeRequest *wbRequest = [WBAuthorizeRequest request];
        //         wbRequest.redirectURI =  sharUrl;
        //         wbRequest.scope = @"all";
        //
        //         WBImageObject *imageObject = [WBImageObject object];
        ////         imageObject.imageData = _iconData;
        //
        //         WBMessageObject *messageObject = [WBMessageObject message];
        ////         messageObject.text = [NSString stringWithFormat:@"%@ %@", self.share_Text,self.share_Link];
        //         messageObject.imageObject = imageObject;
        //
        //         WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:messageObject authInfo:wbRequest access_token:nil];
        //         [WeiboSDK sendRequest:request];
        //
        //                 [WeiboSDK sendRequest:request];
        
    }else if (sender.tag == 4){
        
        
        [self shareWebPageToPlatformType:UMSocialPlatformType_QQ];
        
        
        
    }else{
        UIPasteboard *pab = [UIPasteboard generalPasteboard];
        
        NSString *string = sharUrl;
        
        [pab setString:string];
        
        if (pab == nil) {
            [SVProgressHUD showWithStatus:@"复制失败"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }else
        {
            
            [SVProgressHUD showWithStatus:@"复制成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                
            });
        }
    }
    
}

- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    WEAKSELF
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    //创建网页内容对象
    NSString* thumbURL =  @"";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"贴脸儿" descr:@"欢迎使用贴脸儿App" thumImage:thumbURL];
    //设置网页地址
    shareObject.webpageUrl = sharUrl;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                
                [SVProgressHUD showWithStatus:@"分享成功"];
                
                [_customShareView hideInView];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                
                [HcardHttpmanager getshareTaskWithTaskId:_taskId success:^(id result) {
                    
                    [weakSelf requestDate];
                    
                    
                } failure:^(NSError *error) {
                    
                }];

            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        //        [self alertWithError:error];
    }];
}









@end
